<template>
  <div class="cmt-contaniner">
    <h3>发表评论</h3>
    <hr>
    <textarea placeholder="请输入要BB的内容（最多吐槽120字）" maxlength="120" v-model="msg" cols="30" rows="10"></textarea>

    <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item,i) in comments" :key="item.commentid">
        <div class="cmt-title">
          第{{i+1}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}
        </div>
        <div class="cmt-body">
          {{ item.content === 'undefined' ? '此用户很懒，嘛都没说' : item.content }}
        </div>
      </div>
     
    </div>

    <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
    
  
  </div>
</template>

<script>
  import { Toast } from 'mint-ui'
  import { throws } from 'assert';
  export default {
    data(){
      return {
        pageIndex: 1,  //默认展示第一页数据
        comments: [],  //所有评论数据
        msg: '' //评论输入的内容
      }
    },
    created(){
      this.getComments()
    },
    methods:{
      // JS生成GUID函数,类似.net中的NewID();
      S4() 
      {   
        return (((1+Math.random())*0x10000)|0).toString(16).substring(1);   
      },
      NewGuid() 
      {   
        return (this.S4()+this.S4()+"-"+this.S4()+"-"+this.S4()+"-"+this.S4()+"-"+this.S4()+this.S4()+this.S4());   
      },
      newGuid2()
      {
        var guid = "";
        for (var i = 1; i <= 32; i++){
          var n = Math.floor(Math.random()*16.0).toString(16);
          guid +=   n;
          if((i==8)||(i==12)||(i==16)||(i==20))
            guid += "-";
        }
        return guid;    
      },
      getComments(){
        // 获取评论数据
       /*  this.$http.get("api/getcomments/"+ this.cmtid +"?pageIndex=" + this.pageIndex)
        .then(result => {
          if(result.body.status === 0){
            // this.comments = result.body.message
            // 每单获取新评论数据的时候（加载更多）不要把老数据情况覆盖，而是应该以老数据拼接上个新数据
            this.comments = this.comments.concat(result.body.message)
          }else{
            Toast('获取评论失败！')
          }
        }) */
        // 模拟数据
        var obj1 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"我来评论一下",
        }
        var obj2 = {
         "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"什么东阿",
        }
        var obj3 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"都弄",
        }
        var obj4 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"法官如果",
        }
        var obj5 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"飞哥哥",
        }
        var obj6 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"高考来了",
        }
        var obj7 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"法官如果",
        }
        var obj8 = {
          "commentid" : this.NewGuid(),
          "user_name" : "匿名用户",
          "add_time": Date.now(),
          "content":"或建议看看",
        }
        this.comments.push(obj1)
        this.comments.push(obj2)
        this.comments.push(obj3)
        this.comments.push(obj4)
        this.comments.push(obj5)
        this.comments.push(obj6)
        this.comments.push(obj7)
        this.comments.push(obj8)
      },
      getMore(){ //加载更多
        this.pageIndex++
        this.getComments()
      },
      postComment(){ //发表评论
        if(this.msg.trim().length === 0){
          return Toast("评论内容不能为空！")
        }
        // 参数1：请求的URL地址
        // 参数2：提交给服务器的数据对象 { content: this.msg}
        // 参数3：定义提交时候i，表单中数据的格式 { emulateJSON:true}，在index.js设置
        // 注意：这里获取资讯id可以使用this.$route.params.id也可以使用this.cmtid
       /*  this.$http.post('api/postcomment/' + this.$route.params.id, {content:this.msg.trim()})
        .then(function(result){
          if(result.body.status === 0){
            // 1. 拼接出一个评论对象
            var cmt = { 
              user_name: "匿名用户",
              add_time: Date.now(),
              content: this.msg.trim()
            }
            this.comments.unshift(cmt)
            this.msg = ""
          }
        }) */
        // 模拟发表评论
         var cmt = {
              commentid : this.NewGuid(),
              user_name: "匿名用户",
              add_time: Date.now(),
              content: this.msg.trim()
          }
          this.comments.unshift(cmt)
          this.msg = ""
      }
    },
    props: ["cmtid"]
  }
</script>

<style lang="scss" scoped>
.cmt-contaniner{
  h3{
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin-bottom: 5px;
  }
  .cmt-list{
    margin: 5px 0;
    .cmt-item{
      font-size: 13px;
      .cmt-title{
        line-height: 30px;
        background-color: #ccc;
      }
      .cmt-body{
        line-height: 35px;
        text-indent: 2em;
      }
    }
  }
}
</style>

